package com.fessc.develop.handler.processor.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fessc.develop.base.Result;
import com.fessc.develop.entity.comvo.CommonHeadSendVo;
import com.fessc.develop.entity.dbvo.TOrgUrlManage;
import com.fessc.develop.handler.SignHandler;
import com.fessc.develop.model.enums.*;
import com.fessc.develop.service.interfaces.ITOrgUrlManageService;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Service
public class CommonPlatService implements IBaseService {

    @Resource
    private SignHandler signHandler;

    @Resource
    private ITOrgUrlManageService tOrgUrlManageService;

    @Resource
    private OkHttpUtil okHttpUtil;

    @Override
    public String getServiceCode() {
        return EnumServiceCode.PLAT_MP0000.toString();
    }

    @Override
    public Result process(JSONObject request) {
        log.info("市场交易平台通用接口请求报文: " + request);
        JSONObject bodyObject = request.getJSONObject(EnumJsonProperty.REQ_BODY.toString());
        JSONObject headObject = request.getJSONObject(EnumJsonProperty.REQ_SYS_HEAD.toString());
        CommonHeadSendVo commonHeadSendVo = JSON.parseObject(headObject.toJSONString(), CommonHeadSendVo.class);
        log.info("互联互通通用转发接口请求明文: " + JSON.toJSONString(request));

        //根据机构查询配置表获取URL,通用默认一个机构只有一个URL
        TOrgUrlManage orgUrlManage = tOrgUrlManageService.getOrgUrlByOrgCodeAndKey(commonHeadSendVo.getBankId(), EnumOrgType.BANK.toString(), EnumServiceCode.COM_BS0000.toString());
        // 发送报文body加密
        String bodyDecrypt = signHandler.encrypt(JSON.toJSONString(bodyObject), orgUrlManage.getPubCert());
        request.put(EnumJsonProperty.REQ_BODY.toString(), bodyDecrypt);
        Map<String, String> requestHeaders = okHttpUtil.getRequestHeaders(orgUrlManage.getAppKey(), orgUrlManage.getInterfaceUrl(), commonHeadSendVo.getBankId());
        log.info("互联互通通用转发接口请求密文：" + JSON.toJSONString(request));

        //接口推送
        String responseEncryptJson = "";  //返回加密报文
        JSONObject responseDecryptJson = new JSONObject();  //解密后报文
        try {
            // 先解析返回报文
            responseEncryptJson = okHttpUtil.doPostJson(orgUrlManage.getInterfaceUrl(), JSON.toJSONString(request), requestHeaders);
            responseDecryptJson = JSON.parseObject(responseEncryptJson);
            log.info("通用转发接口响应报文(解密前): " + JSON.toJSONString(responseDecryptJson));
            String date = responseDecryptJson.getString(EnumJsonProperty.RES_DATA.toString());
            // 解密DATA部分
            if (BusiUtil.isNotNull(date)) {
                responseDecryptJson.put(EnumJsonProperty.RES_DATA.toString(), JSONObject.parseObject(signHandler.decrypt(date)));
            }
            log.info("通用转发接口响应报文(解密后): " + JSON.toJSONString(responseDecryptJson));
        } catch (Exception e) {
            log.info("通用转发接口响应报文解析失败: " + e.getMessage());
        }

        // 返回报文
        Result result = new Result(responseDecryptJson.getString(EnumJsonProperty.RES_STATUS_CODE.toString()), responseDecryptJson.getString(EnumJsonProperty.RES_STATUS_MSG.toString()));
        result.setData(responseDecryptJson.getJSONObject(EnumJsonProperty.RES_DATA.toString()));
        log.info("市场交易平台通用接口返回报文: " + JSON.toJSONString(result));
        return result;
    }

}
